﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class FindProjekt : System.Web.UI.Page
{
    private static string path = "";
    private static ProjectBLL pBll = new ProjectBLL();
    private static DivisionBLL dBll = new DivisionBLL();
    private static DivisonProperty dPP = new DivisonProperty();
    private static SubEducationBLL seBll = new SubEducationBLL();
    private static SubEducationProperty sePP = new SubEducationProperty();
    private static List<ProjectProberty> lPP;
    private static int maximumShowRow = 10;
    private static string result;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            path = HttpContext.Current.Request.Url.AbsoluteUri;
            if (!String.IsNullOrEmpty(path.Substring(path.LastIndexOf('/') + 1)))
            {
                lPP = new List<ProjectProberty>();
                result = TitelFinder();
                if (result == "Tværfagligt")
                {
                    GetData();
                }
                else
                {
                    cblDivisions.Visible = false;
                    dPP = dBll.GetDivisionByName(result);
                    bool hasSubEd = false;
                    if (Divergence().Equals("1"))
                    {
                        hasSubEd = true;
                    }

                    lPP = pBll.GetAllProjectsByDivisonID(dPP.Id, hasSubEd);
                }
            }

            FillRepeater(lPP);
        }
    }
    private string TitelFinder()
    {
        string result = "";
        switch (path.Substring(path.LastIndexOf('/') + 1))
        {
            case "Business": result = "Business"; break;
            case "Pedagogue": result = "Pædagogik"; break;
            case "Interdisciplinary": result = "Tværfagligt"; break;
            case "HealthEducation": result = "Sundhed"; break;
            case "Technology": result = "Teknologi"; break;

            default: result = "Er du gået forkert?"; break;
        }
        return result;
    }
    private void FillRepeater(List<ProjectProberty> _lpp)
    {
        PagedDataSource pagedDs = new PagedDataSource();
        pagedDs.DataSource = _lpp;
        pagedDs.AllowPaging = true;
        pagedDs.PageSize = maximumShowRow;
        pagedDs.CurrentPageIndex = NowViewing;
        int page = pagedDs.CurrentPageIndex + 1;
        lblCurrentPage.Text = page.ToString();

        lbtnPrev.Enabled = !pagedDs.IsFirstPage;
        lbtnNext.Enabled = !pagedDs.IsLastPage;

        rpContent.DataSource = pagedDs;
        rpContent.DataBind();
        for (int i = 0; i < pagedDs.Count; i++)
        {
            Label lblDivision = (Label)rpContent.Items[i].FindControl("lblDivision");

            string _name = dBll.GetDivisionByID(Convert.ToInt32(lblDivision.Text)).DivisionName;

            if (!String.IsNullOrEmpty(_name))
            {
                lblDivision.Text = _name;
            }
        }

        if (Divergence().Equals("1"))
        {
            Label lblH = (Label)rpContent.Controls[0].Controls[0].FindControl("lblHeaderCompany");

            lblH.Text = "Uddannelse";

            for (int i = 0; i < pagedDs.Count; i++)
            {
                Label lbl = (Label)rpContent.Items[i].FindControl("lblCompanyName");

                string _name = seBll.GetSubEducationNameByID(Convert.ToInt32(lPP[i].SubEducationId));

                if (!String.IsNullOrEmpty(_name))
                {
                    lbl.Text = _name;
                }
            }

        }

        //for (int i = 0; i < pagedDs.Count; i++)
        //{
        //    LinkButton lbl = (LinkButton)rpContent.Items[i].FindControl("lbtnTitel");
        //    if (lbl.Text.Length > 13)
        //    {
        //        lbl.Text = lbl.Text.Insert(11, "-");
        //    }
            
        //}

        if (result != "Teknologi")
        {
            HideRequirement(true, pagedDs.PageSize);
            HtmlGenericControl div = (HtmlGenericControl)rpContent.Controls[0].Controls[0].FindControl("repeaterHeaderWrapper");
            div.Attributes.Add("class", "indexCompanyHeader");
            for (int i = 0; i < pagedDs.Count; i++)
            {
                HtmlGenericControl divC = (HtmlGenericControl)rpContent.Items[i].FindControl("repeaterContentWrapper");
                divC.Attributes.Add("class", "indexCompanyContent");
            }
        }
        else
        {
            HideRequirement(false, pagedDs.PageSize);
            HtmlGenericControl div = (HtmlGenericControl)rpContent.Controls[0].Controls[0].FindControl("repeaterHeaderWrapper");
            div.Attributes.Add("class", "indexCompanyHeaderT");
            for (int i = 0; i < pagedDs.Count; i++)
            {
                HtmlGenericControl divC = (HtmlGenericControl)rpContent.Items[i].FindControl("repeaterContentWrapper");
                divC.Attributes.Add("class", "indexCompanyContentT");
            }
        }

        if (!lbtnNext.Enabled)
        {
            lbtnNext.Visible = false;
        }
        else
        {
            lbtnNext.Visible = true;
        }
        if (!lbtnPrev.Enabled)
        {
            lbtnPrev.Visible = false;
        }
        else
        {
            lbtnPrev.Visible = true;
        }
    }
    protected void lbtnTitel_Click(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// Link button function is right here.
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rpContent_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Random r = new Random();
        int cookieid = r.Next(1, 9999);
        HttpCookie cookie = new HttpCookie("descId" + cookieid);
        cookie.Expires = DateTime.Now.AddMinutes(10);
        LinkButton lnk = (LinkButton)e.Item.FindControl("lbtnTitel");
        cookie.Value = lnk.CommandArgument.ToString();
        Response.Cookies.Add(cookie);

        Response.Redirect("~/ProjectDescription.aspx/" + cookieid);
    }
    protected void cblDivisions_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetData();
        FillRepeater(lPP);
    }
    private void GetData()
    {
        List<int> divIDs = new List<int>();
        divIDs.Add(5);
        foreach (ListItem item in cblDivisions.Items)
        {
            if (item.Selected)
            {
                divIDs.Add(Convert.ToInt32(item.Value));
            }
            else
            {
                divIDs.Add(0);
            }
        }
        lPP = pBll.GetAllProjects(divIDs);
    }
    private string Divergence()
    {
        return path.Substring(path.LastIndexOf('?') + 1, 1);
    }
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        
        lPP = pBll.SearchForProjectsByTitle(txtSearch.Text, dBll.GetDivisionByName(TitelFinder()).Id);
        if (lPP.Count <= 0)
        {
            lPP = pBll.SearchForProjectsByCompanyName(txtSearch.Text, dBll.GetDivisionByName(TitelFinder()).Id);
        }
        NowViewing = 0;
        FillRepeater(lPP);
    }

    /// <summary>
    /// Show or hide the Requirement colon.
    /// </summary>
    /// <param name="show">True will show the colon, false and it will be hidden.</param>
    private void HideRequirement(bool show, int maxRow)
    {
        int rows = maxRow;
        if (rpContent.Items.Count < maxRow)
        {
            rows = rpContent.Items.Count;
        }

        if (!show)
        {
            var divHR = rpContent.Controls[0].Controls[0].FindControl("colonHRequirement");
            var divHB = rpContent.Controls[0].Controls[0].FindControl("colonHBlank");
            for (int i = 0; i < rows; i++)
            {
                var divCR = rpContent.Items[i].FindControl("colonRequirement");
                var divCB = rpContent.Items[i].FindControl("colonBlank");
                divCR.Visible = true;
                divCB.Visible = false;
            }
            divHR.Visible = true;
            divHB.Visible = false;
        }
        else
        {
            var divHR = rpContent.Controls[0].Controls[0].FindControl("colonHRequirement");
            var divHB = rpContent.Controls[0].Controls[0].FindControl("colonHBlank");
            for (int i = 0; i < rows; i++)
            {
                var divCR = rpContent.Items[i].FindControl("colonRequirement");
                var divCB = rpContent.Items[i].FindControl("colonBlank");
                divCR.Visible = false;
                divCB.Visible = true;
            }
            divHR.Visible = false;
            divHB.Visible = true;
        }
        
    }

    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        NowViewing++;
        FillRepeater(lPP);
    }
    protected void lbtnPrev_Click(object sender, EventArgs e)
    {
        NowViewing--;
        FillRepeater(lPP);
    }
    private int NowViewing
    {
        get
        {
            object obj = ViewState["_NowViewing"];
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return (int)obj;
            }
        }
        set
        {
            this.ViewState["_NowViewing"] = value;
        }
    }
    protected void btnBackward_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "goBack", "history.go(-2);", true);
    }
}